﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TagLib;

namespace MuzičkiFeršterker
{
    /// <summary>
    /// Static class that uses TagLib to fetch ID3 tags
    /// </summary>
    public static class FetchSongInfo
    {
        /// <summary>
        /// Returns Song lentgth in form of a string
        /// </summary>
        /// <param name="path">Path of the song</param>
        /// <returns>mm:ss song length or 00:00 if fetching fails.</returns>
        public static string GetSongLength(string path)
        {
            try
            {
                TagLib.File file = TagLib.File.Create(path);
                return millisecondsToString(file.Properties.Duration.TotalMilliseconds);
            }
            catch
            {
                return "00:00";
            }
        }

        /// <summary>
        /// Gets the song title
        /// </summary>
        /// <param name="path">Path to the song</param>
        /// <returns>Returns the title or empty string if fetching fails</returns>
        public static string GetSongTitle(string path)
        {
            try
            {
                TagLib.File file = TagLib.File.Create(path);
                return file.Tag.Artists[0];
            }
            catch
            {
                return "";
            }
        }

        /// <summary>
        /// Gets song artist
        /// </summary>
        /// <param name="path">Path to the song</param>
        /// <returns>Returns artist or empty string if fetching fails</returns>
        public static string GetSongArtist(string path)
        {
            try
            {
                TagLib.File file = TagLib.File.Create(path);
                return file.Tag.Title;
            }
            catch
            {
                return "";
            }
        }
        
        /// <summary>
        /// Returns song length as integer
        /// </summary>
        /// <param name="path">Path to the song</param>
        /// <returns>Song duration in ms. Returns 0 if fetching fails</returns>
        public static int GetSongLengthINT(string path)
        {
            try
            {
                TagLib.File file = TagLib.File.Create(path);
                return (int)file.Properties.Duration.TotalMilliseconds;
            }
            catch
            {
                return 0;
            }
        }

        /// <summary>
        /// Method used to convert from double time to length string
        /// </summary>
        /// <param name="ms1">time in ms presented in double format</param>
        /// <returns>Returns mm:ss.</returns>
        private static string millisecondsToString(double ms1)
        {
            string vrni = "";
            uint ms = (uint)ms1;
            ms = ms - (ms % 1000);
            ms /= 1000;
            vrni = ms.ToString();
            string minute = (ms / 60).ToString();
            if (minute.Length < 2)
                minute = "0" + minute;
            string sekunde = (ms - (60 * (ms / 60))).ToString();
            if (sekunde.Length < 2)
                sekunde = "0" + sekunde;

            vrni = minute + ":" + sekunde;
            return vrni;
        }
    }
}
